' Find Many Strings
' This program prompts the user for a set number of lines and a search
'   string and then prints the lines and the number of matches found.

' Set maximum number of lines that can be entered and declare string
'   array to hold lines.

maxLines% = 10   ' maxLines% will be shared with the GetText subprogram
DIM inputLines$(maxLines%)

CLS

' Call GetText subprogram to get input from user; at return, the
'   numOfLines% variable will contain number of lines received.

CALL GetText (inputLines$(), numOfLines%)

' Get pattern to be searched for from user.

PRINT
INPUT "Enter the string to be searched for:  ", pattern$
PRINT

' Call Repeat subprogram to determine the number of matches per line.
' The totalRepeats% variable will accumulate the number of total matches.

FOR i% = 1 TO numOfLines%
    CALL Repeat (pattern$, inputLines$(i%), lineRepeats%)
    totalRepeats% = totalRepeats% + lineRepeats%
NEXT i%

' Display lines entered by the userI

PRINT "You entered the following lines:"
PRINT
FOR i% = 1 TO numOfLines%
    PRINT inputLines$(i%)
NEXT i%

' and the total number of matches.

PRINT
PRINT "The pattern '"; pattern$; "' appears"; totalRepeats%; "times."

PRINT
INPUT "Press Return to continue", dummy$

END

SUB GetText (strArray$(), count%) STATIC

' The GetText subprogram fills the strArray$ array with text
'   entered at the keyboard.  The number of lines that can be
'   entered is determined by the shared variable maxLines%.
'   Both strArray$ and count% (the number of lines actually
'   entered) are returned to the main program.

SHARED maxLines%    ' get maxLines% from the main program

PRINT "Enter up to"; maxLines%; "lines of text; to end, ";
PRINT "press Return on a new line."
PRINT

count% = 0                   ' initialize variables to 0 and "empty"
inLine$ = "empty"

' Loop until count% = MAXLINES% or an empty line is received.

WHILE (count% < maxLines%) AND (inLine$ <> "")
    LINE INPUT "-> "; inLine$  ' get line from user
    IF (inLine$ <> "") THEN       ' if line is not blank, copy it
        count% = count% + 1        '   to the strArray$ array
        strArray$(count%) = inLine$
    END IF
WEND

END SUB

SUB Repeat (searchStr$, baseStr$, num%) STATIC
 
' The Repeat subprogram returns the number of times a search string
'   is found in a base string.  The number is returned to the main
'   program through the num% parameter.

searchLength% = LEN(searchStr$)  ' determine length of search string
baseLength% = LEN(baseStr$)        ' determine length of base string
currentChar% = 1                ' character offset in base string
num% = 0                            ' running total of matches found in base

' Loop until entire string is processed or INSTR returns a 0.

WHILE (currentChar% <= baseLength%) AND (currentChar% <> 0)
    currentChar% = INSTR(currentChar%, baseStr$, searchStr$)
    IF (currentChar% <> 0) THEN  ' if not 0, a match was found
        num% = num% + 1                ' increment number of matches
        ' New offset equals current offset plus search-string length.
        currentChar% = currentChar% + searchLength%
    END IF
WEND

END SUB
